package com.pig4cloud.pig.trip.controller;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.trip.Service.TrTripContentService;
import com.pig4cloud.pig.trip.Service.TrTripService;
import com.pig4cloud.pig.trip.api.dto.InquireDTO;
import com.pig4cloud.pig.trip.api.dto.TripDTO;
import com.pig4cloud.pig.trip.api.entity.TrTrip;
import com.pig4cloud.pig.trip.api.entity.TrTripContent;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@RestController
@RequiredArgsConstructor
@RequestMapping("/trip")
@Tag(description = "trip", name = "行程单管理")
//@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TrTripController {
	@Autowired
	private TrTripService trTripService;
    @Autowired
	private TrTripContentService trTripContentService;

	/**
	 * 根据咨询号或手机号模糊查询行程单
	 * @param page 分页参数
	 * @param inquireNumber 咨询号（可选）
	 * @param cusTel 客户手机号（可选）
	 * @return 分页查询结果
	 */
	@GetMapping("/selectByCondition")
	public   R listTripsByInquireNumOrTel(
			Page page,
			@RequestParam(required = false) String inquireNumber,
			@RequestParam(required = false) String cusTel,
			@RequestParam(required = false) String trStatus) {
		return R.ok(trTripService.selectByCondition(page,inquireNumber,cusTel,trStatus));
	}
	@GetMapping("/unUseInquireId")
	public R<List<InquireDTO>> getInquireList() {
		List<InquireDTO> inquireList = trTripService.getUnusedInquireList();
		return R.ok(inquireList);
	}
	/**
	 * 根据咨询单的内容查询相关内容
	 */
	@GetMapping("/getInquireBYId")
	public R getInquireBYId(
			@RequestParam(required = false) Long inquireId) {
		return R.ok(trTripService.getInquireBYId(inquireId));
	}
	/**
	 * 新增一条行程单
	 */
	@PostMapping("/addTrip")
	public R addTrip(@RequestBody TripDTO tripDTO) {
		TrTrip trTrip = new TrTrip();
		trTrip.setInquireId(tripDTO.getInquireId());
		trTrip.setCusId(tripDTO.getCusId());
		trTrip.setTripCosts(tripDTO.getTripCosts());
		trTrip.setServiceFee(tripDTO.getServiceFee());
		trTrip.setTripRemark(tripDTO.getTripRemark());
		// 返回行程内容插入结果或其他合适的结果
		trTripService.insertTrip(trTrip);

		return R.ok(trTrip.getTripId());
	}
	/**
	 * 通过行程单id查询咨询的相关内容
	 */
	@GetMapping("/getInquireByTripId")
	public R getInquireByTripId(
			@RequestParam(required = false) Long tripId) {
		return R.ok(trTripService.getInquireByTripId(tripId));
	}

	/**
	 * 逻辑删除行程单
	 */
	@DeleteMapping("/deleteTrip")
	public R deleteTrip(@RequestParam(required = false) Long tripId) {
		return R.ok(trTripService.deleteTrip(tripId));
	}

	/**
	 * 修改行程单金额
	 * @param tripDTO
	 * @return
	 */
	@PutMapping("/amount") // 或 @PostMapping，需与前端一致
	public R<Integer> updateAmount(@RequestBody TripDTO tripDTO) { // 关键：@RequestBody必须加
		int updateCount = trTripService.updateTripAmount(tripDTO);
		return R.ok(updateCount); // 返回更新条数
	}
}
